/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-16 17:36:20
 * @LastEditors: wiz
 * @LastEditTime: 2023-12-16 11:17:55
 */
/* eslint-disable max-classes-per-file */
import { N_oSetupPanel } from '../../types';
import { T_compPanelConf } from '../compConfPanel';
import { T_toolbarLib } from '../compToolbar';
import { T_compBase } from './compBase';

type I_tableStyle = N_oSetupPanel.N_comps.I_tableStyle;
type I_compToolbar = N_oSetupPanel.N_comps.I_compToolbar;
type I_messageConf = N_oSetupPanel.N_comps.I_messageConf;
export class T_refGraphConf {
  name: string;

  comment: string;

  isCreated: boolean;

  status: string;

  dbType: 'mysql' | 'mongo';

  tableStyle: I_tableStyle;

  // 表格
  tableColumns: Array<any>;

  // 画布节点配置
  nodeColumns: Array<any>;

  uniqueIndexes: Array<any>;

  // 唯一索引
  indexes: Array<any>;

  // 索引
  rules: Array<any>;

  // 规则
  selbill: Array<any>;

  // 选单配置
  queryLink: Array<any>;

  // 联查设置
  action: object;

  // 表头分组
  headerGroup: Array<any>;

  // 图表设置
  charts: Array<any>;

  // 工具栏
  toolbar: I_compToolbar;

  // 表数据处理
  tableDataSetting: {
    editable: boolean;
    autoload: boolean;
  };

  messageConfig: I_messageConf;
}
export class T_refGraph extends T_compBase {
  public conf: T_refGraphConf = new T_refGraphConf();

  private TtoolbarLib: T_toolbarLib = new T_toolbarLib();

  private TcompPanelConf: T_compPanelConf = new T_compPanelConf();

  constructor() {
    super();
    this.conf = {
      name: '',
      comment: '',
      isCreated: false,
      status: '',
      dbType: N_oSetupPanel.N_comps.DB_TYPE.MYSQL,
      tableStyle: {
        showHeader: true,
        showFooter: true,
        showPager: true,
        showToolbar: true,
        border: false,
        mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
      }, // 表格样式
      tableColumns: [],
      nodeColumns: [],
      uniqueIndexes: [], // 唯一索引
      indexes: [], // 索引
      rules: [], // 规则
      selbill: [], // 选单配置
      queryLink: [], // 联查设置
      action: {},
      headerGroup: [], // 表头分组
      charts: [], // 图表设置
      toolbar: null, // 工具栏
      tableDataSetting: {
        editable: true,
        autoload: true
      }, // 表数据处理
      // messageConfig: null
      messageConfig: {
        custDefListener: {
          search: [],
          coordination: null
        },
        emitActions: {
          search: [],
          refresh: []
        },
        eventGroup: null
      }
    };
    this.conf.toolbar = this.TtoolbarLib.getCompToolBarSet(
      N_oSetupPanel.N_comps.COMP_NAME.REF_GRAPH
    );
  }

  public addTableColumn(...p: any[]): void {
    this.conf.tableColumns.push(...p);
  }

  public addNodeColumns(...p: any[]): void {
    this.conf.nodeColumns.push(...p);
  }

  public addUniqueIndexes(...p: any[]): void {
    this.conf.uniqueIndexes.push(...p);
  }

  public addIndexes(...p: any[]): void {
    this.conf.indexes.push(...p);
  }

  public addRules(...p: any[]): void {
    this.conf.rules.push(...p);
  }

  public addHeaderGroup(...p: any[]): void {
    this.conf.headerGroup.push(...p);
  }

  public addCharts(...p: any[]): void {
    this.conf.charts.push(...p);
  }

  public getConf(): T_refGraphConf {
    return this.conf;
  }

  public GetCompPanelConf(): N_oSetupPanel.N_comps.I_setupPanel {
    return this.TcompPanelConf.getCompPanelSet(
      N_oSetupPanel.N_comps.COMP_NAME.REF_GRAPH
    );
  }

  public getComponentId(): N_oSetupPanel.N_comps.COMP_MODULE_ID {
    return N_oSetupPanel.N_comps.COMP_MODULE_ID.REF_GRAPH;
  }

  public getPermissionList(
    config: T_refGraphConf
  ): Array<N_oSetupPanel.I_permission> {
    return [
      {
        type: 'button',
        area: 'toolbar',
        data: this.getToolbarKey(config)
      },

      {
        type: 'selbill',
        area: 'toolbar',
        data: this.getSelbillData(config)
      },
      {
        type: 'customButton',
        area: 'toolbar',
        data: this.getPmsAddonData(config)
      },
      {
        type: 'columns',
        area: 'form',
        data: this.getPmsColumn(config)
      }
    ];
  }

  private getToolbarKey(config: T_refGraphConf): Array<string> {
    return Object.keys(this.conf.toolbar);
  }

  private getSelbillData(config: T_refGraphConf): Array<string> {
    return [];
  }

  private getPmsColumn(config: T_refGraphConf): Array<string> {
    const arr: Array<string> = [];
    config.tableColumns.forEach(col => {
      arr.push(col.field);
    });
    return arr;
  }

  private getPmsAddonData(config: T_refGraphConf): Array<string> {
    const arr: Array<string> = [];
    if (Array.isArray(config?.toolbar?.addon)) {
      config.toolbar.addon.forEach(ctm => {
        arr.push(ctm.title);
        if (ctm.children.length > 0) {
          // eslint-disable-next-line no-use-before-define
          handleAddonData(ctm.children);
        }
      });
    }

    function handleAddonData(children): void {
      for (const child of children) {
        arr.push(child.title);
        if (child.children.length > 0) {
          handleAddonData(child.children);
        }
      }
    }
    return arr;
  }
}
